package com.ruoyi.mes.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.mes.domain.*;
import com.ruoyi.mes.service.ILnyStorageOutService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mes.domain.WkspTaskCard;
import com.ruoyi.mes.service.IWkspTaskService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 车间任务Controller
 * 
 * @author CFL
 * @date 2024-04-28
 */
@RestController
@RequestMapping("/mes/task")
public class WkspTaskController extends BaseController
{
    @Autowired
    private IWkspTaskService wkspTaskService;

    @Autowired
    private ILnyStorageOutService lnyStorageOutService;

    /**
     * 查询车间任务列表
     */
    @PreAuthorize("@ss.hasPermi('mes:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(WkspTask wkspTask)
    {
        startPage();
        List<WkspTask> list = wkspTaskService.selectWkspTaskList(wkspTask);
        return getDataTable(list);
    }

    /**
     * 导出车间任务列表
     */
    @PreAuthorize("@ss.hasPermi('mes:task:export')")
    @Log(title = "车间任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WkspTask wkspTask)
    {
        List<WkspTask> list = wkspTaskService.selectWkspTaskList(wkspTask);
        ExcelUtil<WkspTask> util = new ExcelUtil<WkspTask>(WkspTask.class);
        util.exportExcel(response, list, "车间任务数据");
    }

    /**
     * 获取车间任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('mes:task:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(wkspTaskService.selectWkspTaskById(id));
    }

    /**
     * 查询领料清单
     */
    @PreAuthorize("@ss.hasPermi('mes:task:pick:query')")
    @GetMapping(value = "/pick/{proId}")
    public TableDataInfo getPickingList(@PathVariable("proId") Long proId)
    {

//        List<WkspTask> list = wkspTaskService.selectWkspTaskList(wkspTask);
        return getDataTable(wkspTaskService.getPickingList(proId));
    }

    /**
     * 领料清单新增出库单
     */
    @PreAuthorize("@ss.hasPermi('mes:task:pick:add:query')")
    @Log(title = "出库", businessType = BusinessType.INSERT)
    @PostMapping("/pick/add")
    public AjaxResult out(@RequestBody List<WkspMaterialListDetail> wkspMaterialListDetail)
    {
        return toAjax(lnyStorageOutService.pickingInsertLnyStorageOut(wkspMaterialListDetail));
    }

    /**
     * 新增车间任务
     */
    @PreAuthorize("@ss.hasPermi('mes:task:add')")
    @Log(title = "车间任务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody WkspTask wkspTask)
    {
        return toAjax(wkspTaskService.insertWkspTask(wkspTask));
    }

    /**
     * 拆卡-新增车间任务卡
     */
    @PreAuthorize("@ss.hasPermi('mes:task:card:add')")
    @Log(title = "车间任务卡", businessType = BusinessType.INSERT)
    @PostMapping("/card/add")
    public AjaxResult addCard(@RequestBody List<WkspTaskCard> wkspTaskCard)
    {
        return wkspTaskService.insertWkspTaskCard(wkspTaskCard);
    }

    /**
     * 修改车间任务
     */
    @PreAuthorize("@ss.hasPermi('mes:task:edit')")
    @Log(title = "车间任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WkspTask wkspTask)
    {
        return toAjax(wkspTaskService.updateWkspTask(wkspTask));
    }

    /**
     * 确认开工-修改车间任务
     */
    @Log(title = "车间任务", businessType = BusinessType.UPDATE)
    @PutMapping("/startwork")
    public AjaxResult editStarkWork(@RequestBody WkspTask wkspTask)
    {
        return toAjax(wkspTaskService.updateStartWork(wkspTask));
    }
    /**
     * 删除车间任务
     */
    @PreAuthorize("@ss.hasPermi('mes:task:remove')")
    @Log(title = "车间任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wkspTaskService.deleteWkspTaskByIds(ids));
    }
}
